package com.sky.service.impl;

import com.sky.mapper.OrderMapper;
import com.sky.service.OrderReportService;
import com.sky.vo.OrderReportVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class OrderReportServiceImpl implements OrderReportService {

    @Autowired
    private OrderMapper orderMapper;

    /**
     * 统计指定时间内的有效订单数、总订单数、订单完成率
     * @param begin
     * @param end
     * @return
     */
    @Override
    public OrderReportVO orderStatistics(LocalDate begin, LocalDate end) {
        // 存储日期
        List<LocalDate> dateList = new ArrayList<>();
        dateList.add(begin);

        while(!begin.equals(end)) {
            begin = begin.plusDays(1);
            dateList.add(begin);
        }

        // 每日订单数
        List<Integer> orderCountList = new ArrayList<>();
        // 每日有效订单数
        List<Integer> validOrderCountList = new ArrayList<>();
        for (LocalDate date : dateList) {
            // 开始、结束时间
            LocalDateTime beginTime = LocalDateTime.of(date, LocalTime.MIN);
            LocalDateTime endTime = LocalDateTime.of(date, LocalTime.MAX);

            // 每日订单数总订单数
            Integer totalOrder = orderCount(beginTime, endTime, null);
            // Integer totalOrder = orderMapper.orderByOrderTime(map);
            orderCountList.add(totalOrder);
            // 每日有效订单数有效订单数：指状态为"已完成"的订单
            Integer validOrder = orderCount(beginTime, endTime, 5);
            // Integer validOrder = orderMapper.orderByOrderTime(map);
            validOrderCountList.add(validOrder);
        }
        // 总订单数
        Integer totalOrderCount = 1;
        // Integer totalOrderCount = orderCountList.stream().reduce(Integer::sum).orElse(0);
        for (Integer orderCount : orderCountList) {
            totalOrderCount += orderCount;
        }
        // 有效订单数
        Integer validOrderCount = 0;
        for (Integer validCount : validOrderCountList) {
            validOrderCount += validCount;
        }
        // Integer validOrderCount = validOrderCountList.stream().reduce(Integer::sum).orElse(0);
        // 订单完成率：有效订单数/总订单数 * 100%
        Double orderCompletionRate = 0.0;
        if (totalOrderCount != 0) {
            orderCompletionRate = validOrderCount.doubleValue() / totalOrderCount;
        } else {
            throw new RuntimeException("数学计算中除数不能为0,请检查数据！");
        }

        return OrderReportVO
                .builder()
                .dateList(StringUtils.join(dateList, ","))
                .orderCountList(StringUtils.join(orderCountList, ","))
                .validOrderCountList(StringUtils.join(validOrderCountList, ","))
                .totalOrderCount(totalOrderCount)
                .validOrderCount(validOrderCount)
                .orderCompletionRate(orderCompletionRate)
                .build();
    }

    public Integer orderCount(LocalDateTime beginTime, LocalDateTime endTime, Integer status) {
        Map<String, Object> map = new HashMap<>();
        map.put("beginTime", beginTime);
        map.put("endTime", endTime);
        map.put("status", status);
        return orderMapper.orderByOrderTime(map);
    }
}
